angular.module('moti.controllers.ordersdetail', ['directiveModule', 'moti.services.ordersService', 'moti.directive.modal'])
    .controller('ordersDetailCtrl', ['$scope', '$location', '$routeParams', 'OrdersInterface', '$modal', function($scope, $location, $routeParams, OrdersInterface, $modal){

        //获取单个订单
        OrdersInterface.getOrder($routeParams.orderId).then(function(data){
            data.create_time = moment(data.create_time).format('YYYY-MM-DD');
            data.is_group_desc = data.is_group == true ? "已成团" : "未成团";
            data.isTicket = data.operate.indexOf("ticket") != -1;
            data.isCancel = data.operate.indexOf("cancel") != -1;
            angular.forEach(data.orderLogs, function(value, key) {
                value.create_time = moment(value.create_time).format('YYYY-MM-DD');
            });
            $scope.order = data;
        });

        //出票
        var ticket_modal;
        $scope.getTicket = function(){
            OrdersInterface.getBatchInfo($scope.order.order_id).then(function(data){
                ticket_modal = $modal({
                    id : 'ticket-modal',
                    text : '您是否要出票当前线路下的所有订单？<br />已出票访客人数：' + data.hasTicketVisitorCount + '<br />待出票访客人数：' + data.readyTicketVisitorCount
                });
                ticket_modal.show();
            });
        };
        $("#ticket-btn").off("click").on("click", function(){
            OrdersInterface.addTicket($scope.order.order_id, {}).then(function(data){
                ticket_modal.hide();
                $modal({id : 'tip-modal', text : "正在单张出票中，请稍候查看结果"}).show().autoHide();
                $scope.order.ticket_disabled = true;
            });
        });
        $("#ticket-btn-batch").off("click").on("click", function(){
            OrdersInterface.addTicket($scope.order.order_id, {is_batch : "1"}).then(function(data){
                ticket_modal.hide();
                $modal({id : 'tip-modal', text : "正在批量出票中，请稍候查看结果"}).show().autoHide();
                $scope.order.ticket_disabled = true;
            });
        });

        //退票
        var default_modal;
        $scope.cancelTicket = function(){
            default_modal = $modal({
                id : 'default-modal',
                text : '您是否要取消订单？'
            });
            default_modal.show();
            $("#default-btn").off("click").on("click", function(){
                OrdersInterface.cancelTicket($scope.order.order_id).then(function(data){
                    default_modal.hide();
                    $modal({id : 'tip-modal', text : "取消订单成功"}).show().autoHide();
                    $scope.order.cancel_disabled = true;
                });
            });
        };

    }]);